package com.irrigation.controller;

import com.irrigation.dto.IrrigationStrategyDto;
import com.irrigation.service.IrrigationStrategyService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/irrigation/strategies")
@RequiredArgsConstructor
public class IrrigationStrategyController {

    private final IrrigationStrategyService strategyService;

    @GetMapping
    public ResponseEntity<List<IrrigationStrategyDto>> getAllStrategies() {
        return ResponseEntity.ok(strategyService.getAllStrategies());
    }

    @GetMapping("/{id}")
    public ResponseEntity<IrrigationStrategyDto> getStrategyById(@PathVariable Long id) {
        return ResponseEntity.ok(strategyService.getStrategyById(id));
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<IrrigationStrategyDto>> getStrategiesByUserId(@PathVariable Long userId) {
        return ResponseEntity.ok(strategyService.getStrategiesByUserId(userId));
    }

    @GetMapping("/device/{deviceId}")
    public ResponseEntity<List<IrrigationStrategyDto>> getStrategiesByDeviceId(@PathVariable Long deviceId) {
        return ResponseEntity.ok(strategyService.getStrategiesByDeviceId(deviceId));
    }

    @PostMapping
    public ResponseEntity<IrrigationStrategyDto> createStrategy(@RequestBody IrrigationStrategyDto strategyDto) {
        return ResponseEntity.ok(strategyService.createStrategy(strategyDto));
    }

    @PutMapping("/{id}")
    public ResponseEntity<IrrigationStrategyDto> updateStrategy(
            @PathVariable Long id, 
            @RequestBody IrrigationStrategyDto strategyDto) {
        return ResponseEntity.ok(strategyService.updateStrategy(id, strategyDto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStrategy(@PathVariable Long id) {
        strategyService.deleteStrategy(id);
        return ResponseEntity.noContent().build();
    }
}
